//index.js
const app = getApp();
const db = wx.cloud.database();

Page({
  data: {
    books: [],
    itemViewHieght: 0,
    selectedCategoryId: "",
    categroies: [],
    editStatus: false
  },

  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    this.getCategories();
    this.getSystemInfo();
    this.getBookList();
    this.getAuditStatus();  
    
  },
  getAuditStatus:function(){
    var that = this;
    db.collection("audit_status").get({
      success:function(res){
        //that.data.audit_status = res.data[0].status; 
        app.backgroundAudioManager.src = res.data[0].default_src;
        app.backgroundAudioManager.title = '网盘资料共享';
        app.backgroundAudioManager.epname = '网盘资料共享'; 
        app.currentAudioUrl = res.data[0].default_src;
        app.currentAudioStatus = 'playing'; 

        app.currentAudioList = [res.data[0].default_src];
        app.currentAudioIndex = 0;
      }
    });
  },
  getSystemInfo: function () {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 可使用窗口宽度、高度 
        // 计算主体部分高度,单位为px
        that.setData({
          // second部分高度 = 利用窗口可使用高度 - first部分高度（这里的高度单位为px，所有利用比例将300rpx转换为px）
          itemViewHieght: res.windowHeight
        })
      }
    })
  },
  getBookList: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    var category_id = that.data.selectedCategoryId;
    wx.cloud.callFunction({
      name: 'list_book_by_category',
      data: {
        category_id: category_id
      },
      complete: res => { 
        that.setData({
          books: res.result.data
        });
        wx.hideLoading();
      },
      error: function (e) {
        console.log(e);
      }
    })
  },
  getCategories: function () {
    var that = this;
    db.collection('categories').where({
        public: true
      })
      .get({
        success: function (res) {
          // res.data 是包含以上定义的两条记录的数组 
          that.setData({
            categroies: res.data
          });
        }
      })
  },
  handleTap: function (e) {
    this.setData({
      selectedCategoryId: e.target.id
    });
    //console.log(this.data.selectedCategoryId)
    this.getBookList();
  },
  onClickBook: function (e) {
    wx.navigateTo({
      url: '/pages/detail/detail?_id=' + e.currentTarget.dataset._id,
    });
    /*
    if(this.data.audit_status == 0){
      var url = e.currentTarget.dataset.url;
      wx.showModal({
        title: '',
        content: url,
        success(res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: url
            })
          } else if (res.cancel) {
  
          }
        }
      })
    }else{
     
    } */
  },
  onShareAppMessage: function () {
    return {
      title: '网盘资料共享',
      desc: '',
      path: '/pages/home/home'
    }
  },
  onAdd: function () {
    wx.navigateTo({
      url: '/pages/edit/edit'
    })
  }
})